package io.dcloud.h527baaa6.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import butterknife.BindView;
import io.dcloud.h527baaa6.R;
import io.dcloud.h527baaa6.base.BaseActivity;
import io.dcloud.h527baaa6.bean.XmlBean;


public class WebToobarActivity extends BaseActivity {


    @BindView(R.id.web_container)
    public FrameLayout mWebContainer;
    public WebView mWebView;
    @BindView(R.id.progressBar1)
    ProgressBar mProgressBar1;


    private XmlBean.DataBean.ItemListBean mItemListBean;

    public static void startActivity(Context context, XmlBean.DataBean.ItemListBean bean){
        Intent intent=new Intent(context,VideoActivity.class);
        intent.putExtra("data",bean);
        context.startActivity(intent);
    }

    /**
     * 获取布局Id
     */
    @Override
    protected int getLayout() {
        return R.layout.activity_web_layout;
    }

    /**
     * 获取标题title
     */
    @Override
    protected int getTitleText() {
        return R.string.video_play;
    }

    /**
     * 初始化布局和数据
     */
    @Override
    protected void initViewAndData() {
        mLoadingView.setShowLoading(true);
        mItemListBean = (XmlBean.DataBean.ItemListBean) getIntent().getSerializableExtra("data");
        mToobarTitleView.setTitleText(mItemListBean.getName());
        initWebView(mItemListBean.getContentdat());
    }

    /**
     * 初始化web界面
     */
    private void initWebView(String url) {
        mWebView = new WebView(this);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.setWebChromeClient(new MyWebChromrClient());
        //启用数据库
        mWebView.getSettings().setDatabaseEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        mWebView.getSettings().setGeolocationDatabasePath(dir);
        //启用地理定位
        mWebView.getSettings().setGeolocationEnabled(true);
        //开启DomStorage缓存
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 优先使用缓存
        mWebView.getSettings().setAppCacheMaxSize(20 * 1024 * 1024);// 缓存最多可以有10M
        mWebView.getSettings().setAllowFileAccess(true);// 可以读取文件缓存(manifest生效)
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setTextZoom(100);
        mWebView.setLayoutParams(new FrameLayout
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mWebContainer.addView(mWebView, 0);
        mWebView.loadUrl(url);
    }
    public class MyWebChromrClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mProgressBar1.setVisibility(View.GONE);//加载完网页进度条消失
            } else {
                mProgressBar1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                mProgressBar1.setProgress(newProgress);//设置进度值
            }
        }
    }
    public class MyWebViewClient extends WebViewClient {

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            int w = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            // 重新测量
            mWebView.measure(w, h);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mLoadingView.setShowLoading(false);
                }
            }, 200);

        }

    }


    @Override
    protected void onDestroy() {
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
        mWebContainer.removeAllViews();
        super.onDestroy();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(mContext, "点击了", Toast.LENGTH_LONG).show();
        return true;
    }
}
